package com.sls.web.manager.action.game;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.DownstreamUserInfo;
import com.sls.web.manager.game.querybean.DownstreamUserQuery;
import com.sls.web.manager.game.service.DownstreamUserService;
import com.sls.web.manager.game.service.GamePriceTemplateService;
import com.sls.web.manager.page.Pagination;

/**
 * 下游用户页面处理
 * 
 * @author cx
 * 
 */
public class DownstreamUserAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	@Resource(name = "downstreamUserService")
	private DownstreamUserService downService;
	@Resource(name = "gamePriceTemplateService")
	private GamePriceTemplateService priceTemplateService;
	private String id;
	private DownstreamUserInfo downInfo;
	private DownstreamUserQuery downQuery;
	private Pagination<DownstreamUserInfo> page;
	private List<String> templateNameList;
	private String userNo;
	private String userName;
	private String templateName;

	/**
	 * 新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		downInfo = new DownstreamUserInfo();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = new Date();
		downInfo.setSignTime(sdf.format(d));
		downInfo.setExpireTime(sdf.format(d));
		try {
			templateNameList = priceTemplateService
					.queryTemplateNameList(getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			downInfo.setUserNo(downInfo.getUserNo().trim());
			downInfo.setUserName(downInfo.getUserName().trim());
			downInfo.setBindIPList(downInfo.getBindIPList().trim());
			if (StringUtils.isNotEmpty(downInfo.getContractNo())) {
				downInfo.setContractNo(downInfo.getContractNo().trim());
			}
			if (StringUtils.isNotEmpty(downInfo.getUnitCode())) {
				downInfo.setUnitCode(downInfo.getUnitCode().trim());
			}
			if (StringUtils.isNotEmpty(downInfo.getUnitName())) {
				downInfo.setUnitName(downInfo.getUnitName().trim());
			}
			if (StringUtils.isNotEmpty(downInfo.getRefundUrl())) {
				downInfo.setRefundUrl(downInfo.getRefundUrl());
			}
			if (StringUtils.isNotEmpty(downInfo.getGoodsSourceModUrl())) {
				downInfo.setGoodsSourceModUrl(downInfo.getGoodsSourceModUrl());
			}
			String tencentPriority = downInfo.getTencentPriority();
			if (tencentPriority == null || "".equals(tencentPriority)) {
				tencentPriority = "1"; // 默认IP优先
			}
			downInfo.setTencentPriority(tencentPriority);

			downService.save(downInfo, getCurrentOperator());
			redirectionUrl = "downstream_user!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		try {
			downInfo = downService.queryDownstreamUserById(id,
					getCurrentOperator());
			templateNameList = priceTemplateService
					.queryTemplateNameList(getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return INPUT;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		try {
			downInfo.setBindIPList(downInfo.getBindIPList().trim());
			String tencentPriority = downInfo.getTencentPriority();
			if (tencentPriority == null || "".equals(tencentPriority)) {
				tencentPriority = "1"; // 默认IP优先
			}
			downInfo.setTencentPriority(tencentPriority);

			downService.update(downInfo, getCurrentOperator());
			if (downQuery == null) {
				downQuery = new DownstreamUserQuery();
				downQuery.setPageSize(Constants.INIT_PAGESIZE);
				downQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			if (StringUtils.isNotEmpty(userNo)) {
				downQuery.setUserNo(userNo);
			}
			if (StringUtils.isNotEmpty(userName)) {
				downQuery.setUserName(userName);
			}
			if (StringUtils.isNotEmpty(templateName)) {
				downQuery.setTemplateName(downInfo.getTemplateName());
			}
			return list();
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
			redirectionUrl = "downstream_user!list.htm";
			return SUCCESS;
		}
	}

	/**
	 * 列表显示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		if (downQuery == null) {
			downQuery = new DownstreamUserQuery();
			downQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			downQuery.setPageSize(Constants.INIT_PAGESIZE);
		}
		try {
			String userNo = downQuery.getUserNo();
			String userName = downQuery.getUserName();
			String templateName = downQuery.getTemplateName();
			userNo = FilterStringSpaces.checkStringHasSpace(userNo);
			userName = FilterStringSpaces.checkStringHasSpace(userName);
			templateName = FilterStringSpaces.checkStringHasSpace(templateName);
			downQuery.setUserNo(userNo);
			downQuery.setUserName(userName);
			downQuery.setTemplateName(templateName);
			page = downService.queryDownstraeamUserInfo(downQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 详情显示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		try {
			downInfo = downService.queryDownstreamUserById(id,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "detail";
	}

	/**
	 * 验证用户名是否存在
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkUserNo() throws Exception {
		DownstreamUserInfo info = downService.queryDownstreamUserByUserNo(
				downInfo.getUserNo(), getCurrentOperator());
		if (info != null) {
			message = "false";
		} else {
			message = "true";
		}
		return "ajax";
	}

	public DownstreamUserService getDownService() {
		return downService;
	}

	public void setDownService(DownstreamUserService downService) {
		this.downService = downService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public DownstreamUserInfo getDownInfo() {
		return downInfo;
	}

	public void setDownInfo(DownstreamUserInfo downInfo) {
		this.downInfo = downInfo;
	}

	public DownstreamUserQuery getDownQuery() {
		return downQuery;
	}

	public void setDownQuery(DownstreamUserQuery downQuery) {
		this.downQuery = downQuery;
	}

	public Pagination<DownstreamUserInfo> getPage() {
		return page;
	}

	public void setPage(Pagination<DownstreamUserInfo> page) {
		this.page = page;
	}

	public List<String> getTemplateNameList() {
		return templateNameList;
	}

	public void setTemplateNameList(List<String> templateNameList) {
		this.templateNameList = templateNameList;
	}

	public String getUserNo() {
		return userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

}
